<?php

namespace App\Http\Services;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class Upload
{
//    public function store(Request $request){
//        $path = $request['idea'] . "/" . date("Y") . "/" . date("m") . "/" . date("d");
//        $res = $request->file('file')->store($path);
//        if($res){
//            return msg(200,"","/uploads/".$res,false);
//        }else{
//            return msg(400,"上传失败",'',false);
//        }
//    }

    /**
     * 图片保存
     */
//    public function store(Request $request){
//        $catalog = $request['idea'] . "/" . date("Y") . "/" . date("m") . "/" . date("d");
//        $path = Storage::disk("local")->put($catalog,$request->file('file'));
//        if($path){
//            return msg(200,"","/uploads/".$path,false);
//        }else{
//            return msg(400,"上传失败",'',false);
//        }
//    }

    /**
     * 图片保存
     */
    public function store($file,$idea){
        $pathArr = [];

        //存储的目录
        $catalog = $idea . "/" . date("Y") . "/" . date("m") . "/" . date("d");

        if(is_array($file)){
            foreach($file as $val){
                $res = Storage::disk("public")->put($catalog,$val);
                if($res){
                    $pathArr[] = "/storage/" . $res;
                }else{
                    $pathArr[] = false;
                }
            }
        }else{
            $res = Storage::disk("public")->put($catalog,$file);
            if($res){
                $pathArr[] = "/storage/" . $res;
            }else{
                $pathArr[] = false;
            }
        }
        if(!in_array(false,$pathArr)){
            return $pathArr;
        }else{
            return false;
        }
    }
}
